package com.budwk.app.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.dto.WeatherFactsDTO;
import lombok.Data;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class WeatherUtil {

    public static HttpGet newHttpGet(String url){
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(RequestConfig.custom()
                .setConnectionRequestTimeout(100000000 * 10) //连接池超时
                .setConnectTimeout(1000000 * 10) //连接超时
                .setSocketTimeout(10000000 * 10) // 读取超时
                .build());
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
        return httpGet;
    }


    //七天 urls为城市编号集合
    public static List<List<Day7>> getResult(List<Integer> urls) {
        ArrayList<List<Day7>> list = new ArrayList<>();
        for (Integer u : urls){
            String url = "http://www.weather.com.cn/weather/"+u+".shtml";
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            try (CloseableHttpResponse response = httpClient.execute(WeatherUtil.newHttpGet(url))) {
                String result = EntityUtils.toString(response.getEntity(),"utf-8"); //设置编码，防止乱码
                List<Day7> listDay=new ArrayList<>();
                Document document= Jsoup.parse(result);
                Elements elements;
                for (int i = 0; i < 7; i++) {

                    Day7 day7 = new Day7();
                    day7.setIp(u);
                    elements=document.select("h1");
                    String text=elements.get(i).text();
                    day7.setDate(text.substring(0,text.length()-4));
                    day7.setDay(text.substring(text.length()-3,text.length()-1));

                    elements=document.select("p[class=wea]");
                    day7.setWeather(elements.get(i).text());

                    elements=document.select("p[class=tem]");
                    day7.setHighTemp(elements.get(i).text());
                    day7.setLowTemp(elements.get(i).text());

                    elements=document.select("p[class=win]");
                    Element child = elements.get(i).child(0);
                    day7.setWindDirection1(child.select("span").get(0).attr("title"));
                    day7.setWindDirection2(child.select("span").get(0).attr("title"));
                    day7.setWindSpeed(elements.get(i).select("i").text());
                    listDay.add(day7);
                }
                list.add(listDay);
                listDay.forEach(System.out::println);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return list;
    }

    //实况
    public static List<WeatherFactsDTO> execute(List<Integer> urls) {
        ArrayList<WeatherFactsDTO> list = new ArrayList<>();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        for (Integer u : urls){
            String url = "http://d1.weather.com.cn/sk_2d/"+u+".html?_=" + System.currentTimeMillis();
            HttpGet hg = WeatherUtil.newHttpGet(url);
            hg.setHeader("Referer",
                    "http://www.weather.com.cn/weather1dn/"+u+".shtml");

            String line = "";
            String Sline = "";

            try {
                CloseableHttpResponse response = httpClient.execute(hg);
                BufferedReader bufferedreader = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

                while ((line = bufferedreader.readLine()) != null) {
                    Sline += line+ "\n";
                }

                Sline = Sline.substring(Sline.indexOf('{'));
                WeatherFactsDTO weatherFacts = JSONObject.parseObject(Sline, WeatherFactsDTO.class);
                list.add(weatherFacts);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        list.stream().forEach(System.out::println);
        return list;
    }

    //后24小时
    public static List<List<Observer24>> Observer24(List<Integer> urls){
        List<List<Observer24>> list = new ArrayList<>();
        for (Integer u : urls) {
            String url = "http://www.weather.com.cn/weather/" + u + ".shtml";
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            try (CloseableHttpResponse response = httpClient.execute(WeatherUtil.newHttpGet(url))) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8"); //设置编码，防止乱码
                Document document = Jsoup.parse(result);
                Elements script = document.getElementsByTag("script");
                for (Element e : script) {
                    if (e.data().contains("var observe24h_data = ")) {
                        String data = e.data().replace("var observe24h_data = ", "");
                        data=data.replace(";","");
                        //拿到od2
                        String s = JSONObject.parseObject(JSONObject.parseObject(data).get("od").toString()).get("od2").toString();
                        List<Observer24> observer24s = JSONArray.parseArray(s, Observer24.class);
                        list.add(observer24s);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        list.stream().forEach(System.out::println);
        return list;
    }

    public static void main(String[] args) {
        List<Integer> urls = new ArrayList<>();
        urls.add(101260201);
        execute(urls);
       // Observer24(urls);
        //getResult(urls);
    }
}

@Data
class Day7 {

    Integer ip; //地区编号
    String name;
    String date; //日期
    String day;
    String weather; //天气
    String highTemp; //高温
    String lowTemp; //低温
    String windDirection1; //风向1
    String windDirection2; //风向2
    String windSpeed; //风速(级)
}

@Data
class Observer24 {

    private String od21; //时间
    private String od22; //温度
    private String od23;
    private String od24; //风向
    private String od25; //风力(级)
    private String od26; //降水量 mm
    private String od27; //相对湿度 %
    private String od28;

}


